package com.yyoo.doc.mypdf.builder;

import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.yyoo.doc.html.sax.SaxHtmlElement;
import com.yyoo.doc.html.style.StyleAttr;
import com.yyoo.doc.html.tag.HtmlTag;

import java.util.Objects;

/**
 * 表格单元格
 * @author zzy
 */
public class PdfTableCellBuilder extends PdfElementBuilder {
    /**
     * cell 的默认值
     */
    private final PdfPCell defaultCell = new PdfPCell();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        PdfTableCellBuilder that = (PdfTableCellBuilder) o;
        return Objects.equals(defaultCell, that.defaultCell);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), defaultCell);
    }

    /**
     * 设置 colspan
     * @param cell
     */
    private void setColspan(PdfPCell cell){
        String colspan = this.getHtmlEl().getAttributeValue("colspan");
        if(colspan != null && !colspan.isEmpty()){
            cell.setColspan(Integer.parseInt(colspan));
        }else {
            cell.setColspan(1);
        }
    }

    /**
     * 设置 colspan
     * @param cell
     */
    private void setRowspan(PdfPCell cell){
        String rowspan = getHtmlEl().getAttributeValue("rowspan");
        if(rowspan != null && !rowspan.isEmpty()){
            cell.setRowspan(Integer.parseInt(rowspan));
        }else {
            cell.setRowspan(1);
        }
    }

    private void setHorizontalAlignment(PdfPCell cell){
        if(getStyleMap().containsKey(StyleAttr.HORIZONTAL_ALIGN)){
            cell.setHorizontalAlignment(getHorizontalAlignment());
        }
    }

    private void setVerticalAlignment(PdfPCell cell){
        if(getStyleMap().containsKey(StyleAttr.VERTICAL_ALIGN)){
            cell.setVerticalAlignment(getVerticalAlignment());
        }
    }

    /**
     * 设置单元格高度
     * @param cell
     */
    private void setHeight(PdfPCell cell){
        if(getStyleMap().containsKey(StyleAttr.HEIGHT)){
            cell.setFixedHeight(getHeight());
        }
    }

    @Override
    public Element build() {

        // 只获取标签本身的文本，子标签的文本不获取
        String text = getHtmlEl().getOwnTextString();

        PdfPTable table = (PdfPTable)getParentPdfEl();
        PdfPCell cell = table.getDefaultCell();
        cloneCell(cell,defaultCell);

        setColspan(cell);
        setRowspan(cell);

        // 设置边框宽度
        setBorderWidth(cell);
        // 设置边框颜色
        setBorderColor(cell);
        // 设置 padding
        setPadding(cell);
        // 设置对齐方式
        setHorizontalAlignment(cell);
        setVerticalAlignment(cell);
        setHeight(cell);
        setBackgroundColor(cell);

        // 设置位置
        setPostion(cell);
        if(!isSkipTag()) {
            return new Paragraph(text.trim(),getFont());
        }

        return null;
    }

    /**
     * 判断是否跳过解析
     * 注：跳过解析时，解析结果直接返回 null
     * @return 是否跳过解析
     */
    private boolean isSkipTag(){
        SaxHtmlElement htmlEl = this.getHtmlEl();
        String tagName = htmlEl.getTagName();

        // thead、tbody、tr 或 th、td元素文本为空时，跳过
        return HtmlTag.THEAD.equalsIgnoreCase(tagName) ||
                HtmlTag.TBODY.equalsIgnoreCase(tagName) ||
                HtmlTag.TR.equalsIgnoreCase(tagName);
    }

    /**
     * 拷贝单元格格式
     * @param sourceCell 源单元格
     * @param targetCell 目标单元格
     */
    private void cloneCell(PdfPCell sourceCell,PdfPCell targetCell){
        targetCell.cloneNonPositionParameters(sourceCell);
        targetCell.setFixedHeight(sourceCell.getFixedHeight());
        targetCell.setHorizontalAlignment(sourceCell.getHorizontalAlignment());
        targetCell.setVerticalAlignment(sourceCell.getVerticalAlignment());
        targetCell.setLeft(sourceCell.getLeft());
        targetCell.setBottom(sourceCell.getBottom());
        targetCell.setTop(sourceCell.getTop());
        targetCell.setRight(sourceCell.getRight());
    }

    @Override
    public void afterBuild(Element el) {
        PdfPTable table = (PdfPTable)getParentPdfEl();
        PdfPCell cell = table.getDefaultCell();

        // 设置后要重置单元格格式为 table 构建时创建的格式
        cloneCell(defaultCell,cell);
    }
}
